package com.qf.czh.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.czh.mapper.LeaveMapper;
import com.qf.czh.mapper.UserMapper;
import com.qf.czh.model.AnnualLeave;
import com.qf.czh.model.RespBean;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class LeaveService {
    @Autowired
    LeaveMapper leaveMapper ;
    @Autowired
    UserMapper userMapper;

    public PageInfo getLeaveByPage(int page,int rows,String sort , String order,String startTime,String endTime,String number , String status) {
        Map<String , Object> params = new HashMap<String, Object>();
        params.put("sort", sort);
        params.put("order", order);
        params.put("startTime", startTime);
        params.put("endTime", endTime);
        params.put("number", number);
        params.put("status", status);


        PageHelper.startPage(page, rows);
        List<AnnualLeave> allAnnualLeave = leaveMapper.getAllAnnualLeave(params);
        PageInfo<AnnualLeave> pageInfo = new PageInfo<AnnualLeave>(allAnnualLeave);
//        System.out.println(pageInfo);
        return pageInfo;
    }

    public Integer addLeave(String vacationtype , String startTime , String endTime , String reason){
        Subject subject = SecurityUtils.getSubject();
        String principal = (String) subject.getPrincipal();
        Integer id = userMapper.getIdByUsername(principal); // userid
//        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");//createtime
//        String creatTime = simpleDateFormat.format(new Date());
        Date creatTime = new Date();
        System.out.println(creatTime + "===================");
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");//createtime
        String number = simpleDateFormat.format(creatTime);

        Map<String , Object> params = new HashMap<String, Object>();
        params.put("vacationtype",vacationtype);
        params.put("startTime",startTime);
        params.put("endTime",endTime);
        params.put("reason",reason);
        params.put("creatTime", creatTime);
        params.put("number",number);
        params.put("status",1);
        params.put("userid",id);

        Integer result = null;
        try {
            result = leaveMapper.addAnnualLeave(params);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return  -1;
    }

    public Integer updateLeave(Integer id ,String vacationtype, String startTime, String endTime, String reason) {
        Map<String , Object> params = new HashMap<String, Object>();
        params.put("id",id);
        params.put("vacationtype",vacationtype);
        params.put("startTime",startTime);
        params.put("endTime",endTime);
        params.put("reason",reason);
        try {
            Integer result = leaveMapper.updateLeave(params);
            return  result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return -1;
    }

    public Integer deleteLeave(String[] ids) {
        List<String> list = Arrays.asList(ids);
        try {
            leaveMapper.deleteLeave(list);
        } catch (Exception e) {
            e.printStackTrace();
            return -1;
        }
        return 1;
    }
}
